<template>
  <div class="">
    <crv-table
      :dataList="dataList"
      :tableConfig="tableConfig"
      :listCount="dataCount"
      :show-footer="showFooter"
      v-model:page="pageInfo"
      :title="`${title}列表`"
      @selectionChange="handleSelectionClick"
    >
      <template #headerHandler>
        <el-button
          v-if="createPermission"
          type="primary"
          @click="handleCreateClick"
          >{{ '新增' + title }}</el-button
        >
      </template>
      <template #status="scope">
        <el-button plain :type="scope.row.enable ? 'success' : 'danger'">
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>

      <template #createAt="scope">
        {{ $filters.formatTime(scope.row.createAt) }}
      </template>

      <template #updateAt="scope">
        {{ $filters.formatTime(scope.row.updateAt) }}
      </template>

      <template #handle="scope">
        <div class="handler-btn">
          <el-button
            link
            type="primary"
            v-if="updatePermission"
            @click="handleEditClick(scope.row)"
            >编辑</el-button
          >
          <el-button
            link
            type="primary"
            v-if="deletePermission"
            @click="handleDeleleClick(scope.row)"
            >删除</el-button
          >
        </div>
      </template>

      <!-- 在page-content中动态插入剩余的插槽 -->
      <template
        v-for="item in otherPropLsit"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </crv-table>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, watch, ref, computed } from 'vue'
import { useStore } from 'vuex'
import CrvTable, { ITable } from '@/base-ui/table'
import ToastUtil from '@/utils/toast'
import { tableConfig } from '@/views/main/product/goods/config/content.config'
import { usePermission } from '@/hooks/use-permission'
export default defineComponent({
  name: 'page-content',
  props: {
    tableConfig: {
      type: Object as PropType<ITable>,
      required: true
    },
    pageName: {
      type: String,
      required: true
    },
    title: {
      type: String,
      default: () => '列表'
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  emits: ['selectionChange', 'newBtnClickEvent', 'editBtnEventClick'],
  components: {
    CrvTable
  },

  setup(props, { emit }) {
    const store = useStore()

    //1:权限
    const updatePermission = usePermission(props.pageName, 'update')
    const deletePermission = usePermission(props.pageName, 'delete')
    const createPermission = usePermission(props.pageName, 'create')
    const queryPermission = usePermission(props.pageName, 'query')

    const pageInfo = ref({ currentPage: 1, pageSize: 10 })
    watch(pageInfo, () => getPageData())

    // 2.发送网络请求
    const getPageData = (queryInfo: any = {}) => {
      if (!queryPermission) return
      store.dispatch('system/getPageListAction', {
        pageName: props.pageName,
        queryInfo: {
          offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
          size: pageInfo.value.pageSize,
          ...queryInfo
        }
      })
    }
    getPageData()

    // 3.从vuex中获取数据
    const dataList = computed(() =>
      store.getters[`system/pageListData`](props.pageName)
    )
    const dataCount = computed(() =>
      store.getters[`system/pageListCount`](props.pageName)
    )

    const handleEditClick = (item: any) => {
      console.log('编辑行点击')
      emit('editBtnEventClick', item)
    }

    const handleDeleleClick = (item: any) => {
      console.log('删除的行是', item)
      ToastUtil.alert({ title: '您确定要删除吗?' }).then((res) => {
        if (res) {
          store.dispatch('system/deletePageDataAction', {
            pageName: props.pageName,
            id: item.id
          })
        } else {
          console.log('取消了')
        }
      })
    }

    const handleCreateClick = () => {
      console.log('新建按钮点击')
      emit('newBtnClickEvent')
    }

    const handleSelectionClick = (value: any) => {
      console.log('选择框发生变化', value)
      emit('selectionChange', value)
    }

    //4:获取需要自定义的插槽列表
    const otherPropLsit: any[] = tableConfig.tableProp.filter((item) => {
      if (
        item.slotName &&
        item.slotName != 'status' &&
        item.slotName != 'createAt' &&
        item.slotName != 'updateAt' &&
        item.slotName != 'handle'
      ) {
        return true
      } else {
        return false
      }
    })

    return {
      dataList,
      dataCount,
      pageInfo,
      updatePermission,
      deletePermission,
      createPermission,
      queryPermission,
      otherPropLsit,
      getPageData,
      handleEditClick,
      handleDeleleClick,
      handleCreateClick,
      handleSelectionClick
    }
  }
})
</script>

<style scoped lang="less"></style>
